package org.jason.article.controller;

import cn.dev33.satoken.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jason.article.service.ICaArticleService;
import org.jason.common.satoken.annotation.SaInteriorOrElse;
import org.jason.model.article.dtos.ContributeDto;
import org.jason.model.article.dtos.ExamineDto;
import org.jason.model.article.dtos.GetArticlesDto;
import org.jason.model.article.es.ArticleContent;
import org.jason.model.article.pojos.CaArticle;
import org.jason.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 文章详情表 前端控制器
 * </p>
 *
 * @author jason
 * @since 2023-07-23
 */
@Api("文章接口")
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ICaArticleService articleService;

    //根据id获取文章
    @ApiOperation("根据id获取文章")
    @SaIgnore
    @GetMapping("/{id:^[1-9]\\d*$}")
    public ResponseResult getArticleById(@PathVariable("id") Integer id) {
        return articleService.getArticleById(id);
    }

    @ApiOperation("根据id获取文章正文")
    @SaIgnore
    @GetMapping("/content/{articleId:^[1-9]\\d*$}")
    public ResponseResult<ArticleContent> getArticleContent(@PathVariable Integer articleId) {
        return articleService.getArticleContent(articleId);
    }

    @ApiOperation("获取文章")
    @SaIgnore
    @PostMapping("/list")
    public ResponseResult getArticles(@RequestBody @Validated GetArticlesDto dto) {
        return articleService.getArticles(dto);
    }

    @ApiOperation("根据用户id获取文章列表")
    @SaCheckLogin
    @GetMapping("/list/{userId:^[1-9]\\d*$}")
    public ResponseResult<List<CaArticle>> getArticlesByUserId(@PathVariable Integer userId) {
        return articleService.getArticlesByUserId(userId);
    }

    @ApiOperation("投稿")
    @SaCheckLogin
    @PostMapping("/contribute")
    public ResponseResult contribute(@RequestBody @Validated ContributeDto dto) {
        return articleService.contribute(dto);
    }

    @ApiOperation("获取待审核文章列表")
    @SaInteriorOrElse
    @SaCheckOr(role = @SaCheckRole("admin"), permission = @SaCheckPermission("examine_article"))
    @GetMapping("/tobeExamine")
    public ResponseResult<List<CaArticle>> getToBeExamineArticle() {
        return articleService.getToBeExamineArticle();
    }

    @ApiOperation("审核文章")
    @SaInteriorOrElse
    @SaCheckOr(role = @SaCheckRole("admin"), permission = @SaCheckPermission("examine_article"))
    @PostMapping("/tobeExamine")
    public ResponseResult examine(@RequestBody @Validated ExamineDto dto){
        return articleService.examineArticle(dto);
    }
}
